package com.kn.sys.service.impl;

import com.kn.common.model.R;
import com.kn.sys.dao.UserRoleDao;
import com.kn.sys.dto.UserRole;
import com.kn.sys.mapper.UserMapper;
import com.kn.sys.mapper.UserRoleMapper;
import com.kn.sys.model.User;
import com.kn.sys.model.UserRoleExample;
import com.kn.sys.service.UserRoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author zhoudf
 * @create 2021-03-09 15:36
 */
@Service
@RequiredArgsConstructor
public class UserRoleServiceImpl implements UserRoleService {
private final UserRoleDao userRoleDao;
private final UserRoleMapper userRoleMapper;
private final UserMapper userMapper;
@Transactional
    @Override
    public R saveByUserId(Long userId, String roleNames, UserRole[] userRoles) {
        UserRoleExample example = new UserRoleExample();
        example.createCriteria().andUseridEqualTo(userId);
        userRoleMapper.deleteByExample(example);
        User user = new User();
        user.setId(userId);
        user.setRoles(roleNames);
        userMapper.updateByPrimaryKeySelective(user);
        if(!roleNames.equalsIgnoreCase("-1")){
            userRoleDao.insert(userRoles);
        }


        return R.ok();
    }
}
